package com.doublefish.doublefishrobot.ui.video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.ui.video.utils.VideoManager;
import com.doublefish.videotrimmer.VideoTrimmer;
import com.doublefish.videotrimmer.interfaces.OnTrimVideoListener;
import com.doublefish.videotrimmer.interfaces.OnVideoListener;

import butterknife.BindView;

/**
 * 作者:dugaofeng on 2024/11/1 16:55
 */
public class VideoEditActivity extends BaseActivity  implements OnTrimVideoListener, OnVideoListener {

    @BindView(R.id.timeLine)
    VideoTrimmer videoTrimmer;


    public static Intent createIntent(Activity context, String path) {
        Intent intent = new Intent(context, VideoEditActivity.class);
        intent.putExtra("video", path);
        return intent;
    }

    @Override
    protected void onDestroy() {
        videoTrimmer.destroy();
        super.onDestroy();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_edit;
    }

    @Override
    protected void initView() {
        //视频路径
        String srcPath = getIntent().getStringExtra("video");
        String destPath = VideoManager.getInstance().getEditCacheVideoDir();

//        videoTrimmer.setMaxDuration(10);  //剪辑的最大时长不限制
        videoTrimmer.setOnTrimVideoListener(this);
        videoTrimmer.setOnVideoListener(this);
        videoTrimmer.setVideoURI(Uri.parse(srcPath));
        videoTrimmer.setDestinationPath(destPath);
                
        videoTrimmer.setVideoInformationVisibility(true);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    //region OnTrimVideoListener

    @Override
    public void onTrimStarted() {
        //开始剪辑
    }

    @Override
    public void cancelAction() {
        finish();
    }

    @Override
    public void onError(String msg) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                finish();

                Toast.makeText(VideoEditActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void getResult(Uri uri) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent data = new Intent();
                data.setData(uri);
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }

    //endregion

    //region OnVideoListener
    @Override
    public void onVideoPrepared() {
        //视频已准备好 可以开始剪辑了
    }
    //endregion

}
